package svc

import (
	"errors"
	"fmt"
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
	"time"
)

// EnterpriseAccount 企业金融账户
type EnterpriseAccount struct {
	UserBll              *bll.User              `inject:""`
	Enterprise           *bll.Enterprise        `inject:""`
	EnterpriseAccountBll *bll.EnterpriseAccount `inject:""`
}

// AccountDetail 账户详情
func (a *EnterpriseAccount) AccountDetail(ctx *context.Context) error {
	ctx.WriteOperateLog("EnterpriseAccount.AccountDetail", "服务平台-企业账户管理-查询账户信息")
	var (
		user              *schema.User
		userCreator       *schema.User
		enterprise        *schema.Enterprise
		enterpriseAccount *schema.EnterpriseFinanceAccount
		err               error
	)

	//获取用户详情
	//绑定张三账号:5bbc09c21d41c843c1b0315b，企业:a116e9f3-d0ec-4be8-b2a0-1295aaae45c4
	user, err = a.UserBll.Get(ctx.GetUserID())
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	//校验用户的企业信息
	if user.EnterpriseID == "" {
		return ctx.ResErrInternalServer(errors.New("用户的企业信息不存在"))
	}
	//查询企业信息
	enterprise, err = a.Enterprise.Get(user.EnterpriseID)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	fmt.Println("enterprise:", enterprise)
	//查询企业的金融账户
	enterpriseAccount, err = a.EnterpriseAccountBll.AccountDetail(user.EnterpriseID)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	fmt.Println("enterpriseAccount:", enterpriseAccount)
	//构造返回结果
	result := make(map[string]interface{})
	result["record_id"] = enterpriseAccount.RecordID
	result["account_title"] = enterpriseAccount.AccountTitle
	result["account_type"] = enterpriseAccount.AccountType
	result["account_balance"] = enterpriseAccount.AccountBalance
	result["account_state"] = enterpriseAccount.AccountState
	result["account_create_time"] = enterpriseAccount.AccountCreateTime
	result["enterprise_id"] = enterpriseAccount.EnterpriseID
	result["enterprise_name"] = enterprise.Name
	result["creator_id"] = enterpriseAccount.AccountCreator
	//查询金融账户的创建人
	userCreator, err = a.UserBll.Get(enterpriseAccount.AccountCreator)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	result["creator_name"] = userCreator.RealName

	return ctx.ResSuccess(result)
}

// QueryPage 查询充值或者消费记录的列表
func (a *EnterpriseAccount) QueryPage(ctx *context.Context) error {
	ctx.WriteOperateLog("EnterpriseAccount.QueryPage", "服务平台-企业账户管理-查询账户充值消费记录列表")
	var (
		pageIndex           uint
		pageSize            uint
		enterpriseID        string
		enterpriseAccountID string
		listAccountLogs     []*schema.BalanceOfPayments
		enterpriseAccount   *schema.EnterpriseFinanceAccount
		total               int
		err                 error
	)
	//获取参数
	pageIndex, pageSize = ctx.GetPageIndex(), ctx.GetPageSize()
	enterpriseID = ctx.Query("enterprise_id")
	enterpriseAccountID = ctx.Query("enterprise_account_id")
	//校验账户信息和请求参数
	enterpriseAccount, err = a.EnterpriseAccountBll.AccountDetail(enterpriseID)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	if enterpriseAccount.RecordID != enterpriseAccountID {
		return ctx.ResErrInternalServer(errors.New("金融账户参数错误"))
	}
	//查询企业的账务记录
	listAccountLogs, total, err = a.EnterpriseAccountBll.AccountPaymentList(enterpriseAccountID, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	fmt.Println("total:", total)
	result := map[string]interface{}{
		"list": listAccountLogs,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// AccountOpen 账户开通
func (a *EnterpriseAccount) AccountOpen(ctx *context.Context) error {
	ctx.WriteOperateLog("EnterpriseAccount.AccountOpen", "服务平台-企业账户管理-开通账户")
	var (
		userDetail *schema.User
		err        error
	)

	//获取用户详情
	//绑定张三账号，企业:a116e9f3-d0ec-4be8-b2a0-1295aaae45c4
	userDetail, err = a.UserBll.Get(ctx.GetUserID())
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	//检查用户类型
	if userDetail.UserType != "40" {
		return ctx.ResErrBadRequest(errors.New("非法操作(非管理员)"))
	}
	//获取请求参数
	var item schema.EnterpriseFinanceAccount
	if err := ctx.ParseJSON(&item); err != nil { //实际需要客户端传递的参数为:enterprise_id account_title account_type
		return ctx.ResErrBadRequest(err)
	}
	//校验员工所在企业和请求开通的企业是否相同
	if userDetail.EnterpriseID != item.EnterpriseID {
		return ctx.ResErrBadRequest(errors.New("企业信息不一致"))
	}
	item.AccountCreator = ctx.GetUserID()
	item.RecordID = util.UUID()
	item.AccountCreateTime = time.Now().Unix()
	item.ID = 0
	item.AccountBalance = 0
	item.LastPaymentID = ""
	item.AccountClosedTime = 0
	item.AccountState = 1

	err = a.EnterpriseAccountBll.AccountOpen(&item)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}
	return ctx.ResOK()
}

// AccountPay 账户充值
func (a *EnterpriseAccount) AccountPay(ctx *context.Context) error {
	ctx.WriteOperateLog("EnterpriseAccount.AccountCharge", "服务平台-企业账户管理-账户充值")
	//
	var (
		userDetail *schema.User
		err        error
	)

	//获取用户详情
	//绑定张三(管理员)账号record_id:5bbc09c21d41c843c1b0315b，所在企业record_id:a116e9f3-d0ec-4be8-b2a0-1295aaae45c4
	//企业金融账户:082f7eb5-5b9d-4d63-a2c2-03ebe91f363e
	userDetail, err = a.UserBll.Get(ctx.GetUserID())

	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	//获取请求参数
	var item schema.BalanceOfPayments
	if err := ctx.ParseJSON(&item); err != nil { //实际需要客户端传递的参数为:enterprise_id enterprise_account_id payment_amount payment_type
		return ctx.ResErrBadRequest(err)
	}
	//充值的情况下:检查用户类型
	if item.PaymentType == 1 {
		//检查用户类型
		if userDetail.UserType != "40" {
			return ctx.ResErrBadRequest(errors.New("非法操作(非管理员)"))
		}
	}

	//校验员工所在企业和请求开通的企业是否相同
	if userDetail.EnterpriseID != item.EnterpriseID {
		return ctx.ResErrBadRequest(errors.New("企业信息不一致"))
	}
	item.ID = 0
	item.RecordID = util.UUID()
	item.PaymentTime = time.Now().Unix()
	item.PayerID = ctx.GetUserID()

	err = a.EnterpriseAccountBll.AccountPay(&item)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}
	return ctx.ResOK()
}

// AccountClose 账户关闭
func (a *EnterpriseAccount) AccountClose(ctx *context.Context) error {
	ctx.WriteOperateLog("EnterpriseAccount.AccountClose", "服务平台-企业账户管理-账户关闭")
	return nil
}
